package com.smh.demo1.a06;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.util.StringValueResolver;

/**
 * @author shiminghui
 * @date 2025/4/12 9:52
 * @description: TODO
 */
// 实现了一些接口,这些接口的方法会在容器初始化时之前调用,顺序为: setBeanName,setApplicationContext,afterPropertiesSet
public class Bean1 implements BeanNameAware, ApplicationContextAware, InitializingBean, EmbeddedValueResolverAware {
    private static final Logger log = LoggerFactory.getLogger(Bean1.class);

    /**
     * 这些set值的操作,@autowired也可以实现,为啥还要实现这些接口呢?
     * 答: 因为@autowired注解的解析要用到BeanPostProcessor,而这些接口可以直接被Spring容器调用,不需要容器添加Bean后处理器
     * 同时,Bean后处理器可能会失效,但是这个接口方法不会生效
     */

    @Override
    public void setBeanName(String s) {
        log.info("Bean1 setBeanName:{}", s);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("Bean1 初始化");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("Bean1 setApplicationContext:{}", applicationContext);
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        log.info("Bean1 setEmbeddedValueResolver:{}", resolver);
    }

    @Autowired
    public void setApplicationContext2(ApplicationContext applicationContext) {
        log.info("Bean1 setApplicationContext use Autowired:{}", applicationContext);
    }

}
